// pages/house/house.js
// const list =[]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    has_police_room: [
      {
        value: 0,
        name: '否',
        selected: false
      },
      {
        value: 1,
        name: '是',
        selected: false
      },
    ],
    has_duty_room: [
      {
        value: 0,
        name: '否',
        selected: false
      },
      {
        value: 1,
        name: '是',
        selected: false
      },
    ],
    close_remark: [
      {
        value: 0,
        name: '全封闭',
        selected: false
      },
      {
        value: 1,
        name: '半封闭',
        selected: false
      },
      {
        value: 2,
        name: '全开放',
        selected: false
      },
    ],
    danger_place: [
      {
        value: 0,
        name: '否',
        selected: false
      },
      {
        value: 1,
        name: '是',
        selected: false
      },
    ],
    e_monitor_install: [
      {
        value: 0,
        name: '否',
        selected: false
      },
      {
        value: 1,
        name: '是',
        selected: false
      },
    ],
    rawData: [],//后台获取的原始数组
    multiObjArray: [], //整理后在页面循环的数组
    multiIndex: [0, 0],
    flag: 0,
    comArr: [],
    gridArr: [],
    newArr: [],
    g_id: '',
    c_id: '',
  },

  onLoad(opts) {
  },

  onShow() {
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];   //上一页
    prevPage.setData({
      status: 1
    })
  },

  policeChange(e) {
    console.log(e)
    let item1 = this.data.has_police_room
    for (let i = 0, len = item1.length; i < len; ++i) {
      item1[i].checked = item1[i].value === e.detail.value
    }
    this.setData({
      item1
    })
  },

  // policeChange(e){
  //   console.log(e)
  // },

  dutyChange(e) {
    let item1 = this.data.has_duty_room
    for (let i = 0, len = item1.length; i < len; ++i) {
      item1[i].checked = item1[i].value === e.detail.value
    }
    this.setData({
      item1
    })
  },

  eChange(e) {
    let item1 = this.data.e_monitor_install
    for (let i = 0, len = item1.length; i < len; ++i) {
      item1[i].checked = item1[i].value === e.detail.value
    }
    this.setData({
      item1
    })
  },

  remarkChange(e) {
    let item1 = this.data.close_remark
    for (let i = 0, len = item1.length; i < len; ++i) {
      item1[i].checked = item1[i].value === e.detail.value
    }
    this.setData({
      item1
    })
  },

  dangerChange(e) {
    let item1 = this.data.danger_place
    for (let i = 0, len = item1.length; i < len; ++i) {
      item1[i].checked = item1[i].value === e.detail.value
    }
    this.setData({
      item1
    })
  },

  formSubmit(e) {
    console.log(e)
    let { h_name, households_num, resident_num, building_num, carport_num, active_place, fit_machine, defire_machine, desteal_machine, clean_machine_remark, advertise_machine_remark, cleaner, has_police_room, has_duty_room, e_monitor_install, close_remark, danger_place, property_company, property_company_master, contact_tel, protecter_num } = e.detail.value
    let access_token = wx.getStorageSync('access_token')
    if (e.detail.value.h_name == '') {
      wx.showToast({
        title: '小区名称不能为空！',
        icon: 'none'
      })
      return false
    }
    if (e.detail.value.building_num == '') {
      wx.showToast({
        title: '楼栋数不能为空！',
        icon: 'none'
      })
      return false
    }
    if (e.detail.value.property_company == '') {
      wx.showToast({
        title: '物业公司名称不能为空！',
        icon: 'none'
      })
      return false
    }
    if (e.detail.value.property_company_master == '') {
      wx.showToast({
        title: '物业公司负责人不能为空！',
        icon: 'none'
      })
      return false
    }
    wx.showLoading({
      title: '正在提交...',
      mask: true
    })
    wx.request({
      url: 'https://baokang.wshoto.com/wx/houseadd',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        access_token: access_token,
        h_name: h_name,
        households_num: households_num,
        resident_num: resident_num,
        building_num: building_num,
        carport_num: carport_num,
        active_place: active_place,
        fit_machine: fit_machine,
        defire_machine: defire_machine,
        desteal_machine: desteal_machine,
        clean_machine_remark: clean_machine_remark,
        advertise_machine_remark: advertise_machine_remark,
        cleaner: cleaner,
        has_police_room: has_police_room,
        has_duty_room: has_duty_room,
        e_monitor_install: e_monitor_install,
        close_remark: close_remark,
        danger_place: danger_place,
        property_company: property_company,
        property_company_master: property_company_master,
        contact_tel: contact_tel,
        protecter_num: protecter_num
      },
      success: (res) => {
        wx.hideLoading()
        if (res.data.code == 1) {
          wx.showModal({
            content: res.data.msg,
            showCancel: false,
            success: (res) => {
              if (res.confirm) {
                wx.navigateBack()
              }
            }
          })
        } else {
          wx.showModal({
            content: res.data.msg,
            showCancel: false,
          })
        }
      }
    })

  }
})